//
//  GKZYRankingListViewController.m
//  GKZYAid
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYRankingListViewController.h"
#import "GKZYRankingListModel.h"
#import "GKZYRankingTableViewCell.h"
#import <AFNetworking.h>
#import "MyDefine.h"
#import "Urls.h"
#import "GKZYSortLeftBarButtonItemView.h"
#import "GKZYRankingWebViewController.h"

@interface GKZYRankingListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *rankingListTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation GKZYRankingListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.rankingListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, VCWIDTH, VCHEIGHT) style:(UITableViewStylePlain)];
    self.rankingListTableView.dataSource = self;
    self.rankingListTableView.delegate = self;
    [self.view addSubview:self.rankingListTableView];
    self.navigationItem.title = @"热门排行榜";
    [self makeData];
    
    GKZYSortLeftBarButtonItemView *leftBBI = [[GKZYSortLeftBarButtonItemView alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    UITapGestureRecognizer *tap6 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
    [leftBBI addGestureRecognizer:tap6];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithCustomView:leftBBI];
    self.navigationItem.leftBarButtonItem = bar;

    // Do any additional setup after loading the view.
}

-(void)back{

    [self.navigationController popViewControllerAnimated:YES];
  
}



-(void)makeData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain", nil];
[manager GET:KrankingURL parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
    
    self.dataArray = [[NSMutableArray alloc]init];
    NSArray *array = responseObject[@"papers"];
    for (NSDictionary *dict in array) {
        GKZYRankingListModel *model = [[GKZYRankingListModel alloc]init];
        [model setValuesForKeysWithDictionary:dict];
        [self.dataArray addObject:model];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.rankingListTableView reloadData];
    });

    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"网络连接失败，失败代码为:%@",error);
}];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return self.dataArray.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    GKZYRankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[GKZYRankingTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    GKZYRankingListModel *model = self.dataArray[indexPath.row];
    [cell configureTheCellWithModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GKZYRankingListModel *model = self.dataArray[indexPath.row];
    GKZYRankingWebViewController *webVC = [[GKZYRankingWebViewController alloc]init];
    webVC.webURl = model.paper_url;
    webVC.titile = model.paper_title;
    [self.navigationController pushViewController:webVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
